<template>
  <div class="login-bg">
    <div class="login-bg-gradient"></div>
    <div class="login-brand">
      <div class="brand-logo-wrap">
        <img src="../assets/logo.png" alt="logo" class="logo-img" />
      </div>
      <div class="brand-name">VUE-VANT</div>
    </div>
    <div class="login-header">欢迎登录</div>
    <div class="login-form">
      <div class="login-tabs">
        <div
          :class="['login-tab', loginType === 'account' && 'active']"
          @click="loginType = 'account'"
        >
          账号登录
        </div>
        <div
          :class="['login-tab', loginType === 'phone' && 'active']"
          @click="loginType = 'phone'"
        >
          手机号登录
        </div>
        <div
          class="tab-slider"
          :style="{ left: loginType === 'account' ? '0%' : '50%' }"
        ></div>
      </div>
      <transition name="fade-slide" mode="out-in">
        <div v-if="loginType === 'account'" key="account">
          <van-field
            v-model="username"
            label="账号"
            placeholder="请输入账号"
            clearable
            :border="false"
          >
            <template #left-icon>
              <van-icon name="user-o" />
            </template>
          </van-field>
          <van-field
            v-model="password"
            label="密码"
            type="password"
            placeholder="请输入密码"
            clearable
            :border="false"
          >
            <template #left-icon>
              <van-icon name="lock" />
            </template>
          </van-field>
          <van-button round block type="info" class="login-btn" @click="onLogin"
            >登 录</van-button
          >
        </div>
        <div v-else key="phone">
          <van-field
            v-model="phone"
            label="手机号"
            placeholder="请输入手机号"
            clearable
            maxlength="11"
            :border="false"
          >
            <template #left-icon>
              <van-icon name="phone-o" />
            </template>
          </van-field>
          <van-field
            v-model="code"
            label="验证码"
            placeholder="请输入验证码"
            clearable
            :border="false"
          >
            <template #left-icon>
              <van-icon name="comment-o" />
            </template>
            <template #button>
              <van-button
                size="small"
                type="primary"
                @click="onSendCode"
                :disabled="countdown > 0"
                >{{
                  countdown > 0 ? countdown + "s" : "获取验证码"
                }}</van-button
              >
            </template>
          </van-field>
          <van-button
            round
            block
            type="info"
            class="login-btn"
            @click="onPhoneLogin"
            >登 录</van-button
          >
        </div>
      </transition>
      <div class="login-divider">
        <span class="divider-line"></span>
        <span class="divider-text">其他方式登录</span>
        <span class="divider-line"></span>
      </div>
      <div class="login-other">
        <div class="other-btn wechat" @click="onWxLogin">
          <svg viewBox="0 0 40 40" width="48" height="48">
            <circle cx="20" cy="20" r="20" fill="#4fcf4f" />
            <path
              d="M13 25c-2.5-1.5-4-3.7-4-6.2C9 14.2 13.6 11 19 11c5.4 0 10 3.2 10 7.8 0 2.5-1.5 4.7-4 6.2l.7 2.2c.1.3-.2.6-.5.5l-2.5-1c-.5.1-1.1.1-1.7.1-5.4 0-10-3.2-10-7.8z"
              fill="#fff"
            />
          </svg>
        </div>
        <div class="other-btn alipay" @click="$toast('暂未开通')">
          <svg viewBox="0 0 40 40" width="48" height="48">
            <circle cx="20" cy="20" r="20" fill="#1677ff" />
            <text
              x="50%"
              y="55%"
              text-anchor="middle"
              fill="#fff"
              font-size="18"
              font-family="Arial"
              dy=".3em"
            >
              支
            </text>
          </svg>
        </div>
      </div>
    </div>
    <div class="login-footer">
      <span class="login-link" @click="$router.push('/forgotPassword')"
        >忘记密码?</span
      >
      <span class="login-link" @click="$router.push('/register')"
        >没有账号？去注册</span
      >
    </div>
  </div>
</template>

<script>
import service from "@/utils/request.js";
import { getSmsCode } from "@/api/api.js";

export default {
  name: "login",
  data() {
    return {
      loginType: "account",
      username: "admin",
      password: "123456",
      phone: "13800138000",
      code: "",
      countdown: 0,
      timer: null,
    };
  },
  methods: {
    async onLogin() {
      if (!this.username || !this.password) {
        this.$toast("请输入账号和密码");
        return;
      }
      try {
        const res = await service.post(
          "/oauth/token",
          new URLSearchParams({
            grant_type: "password",
            username: this.username,
            password: this.password,
            client_id: "client-h5",
            client_secret: "123456",
            scope: "all",
          }),
          {
            headers: {
              "Content-Type": "application/x-www-form-urlencoded",
            },
          }
        );
        const token = res.data && res.data.token;
        const refreshToken = res.data && res.data.refreshToken;
        const tokenHead = (res.data && res.data.tokenHead) || "Bearer ";
        if (token) {
          localStorage.setItem("token", token);
          localStorage.setItem("refresh_token", refreshToken);
          localStorage.setItem("token_head", tokenHead);
          this.$toast("登录成功");
          this.$router.push("/home");
        } else {
          this.$toast("登录失败");
        }
      } catch (e) {
        this.$toast("用户名或密码错误");
      }
    },
    onPhoneLogin() {
      if (!/^1\d{10}$/.test(this.phone)) {
        this.$toast("请输入正确的手机号");
        return;
      }
      if (!this.code) {
        this.$toast("请输入验证码");
        return;
      }
      localStorage.setItem("token", "mock-token");
      this.$toast("登录成功");
      this.$router.push("/home");
    },
    onSendCode() {
      if (!/^1\d{10}$/.test(this.phone)) {
        this.$toast("请输入正确的手机号");
        return;
      }
      getSmsCode({ phone: this.phone }).then((res) => {
        if (res.status == 0) {
          this.$toast("验证码已发送");
          this.countdown = 60;
          this.timer = setInterval(() => {
            this.countdown--;
            if (this.countdown <= 0) {
              clearInterval(this.timer);
            }
          }, 1000);
        } else {
          console.log(res);
          this.$toast(res.message);
        }
      });
    },
    onWxLogin() {
      this.$toast("微信登录开发中...");
    },
  },
  beforeDestroy() {
    if (this.timer) clearInterval(this.timer);
  },
};
</script>

<style scoped>
.login-bg {
  min-height: 100vh;
  background: #f6f8fa;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding-top: 0;
  position: relative;
  overflow: hidden;
}
.login-bg-gradient {
  position: absolute;
  top: 0;
  left: 0;
  width: 100vw;
  height: 50%;
  background: linear-gradient(135deg, #1677ff 0%, #42d0ea 100%);
  z-index: 0;
  border-bottom-left-radius: 60% 30%;
  border-bottom-right-radius: 60% 30%;
  filter: blur(0.5px);
}
.login-bg::before {
  content: "";
  position: absolute;
  top: 40px;
  left: 50%;
  transform: translateX(-50%);
  width: 220px;
  height: 220px;
  background: radial-gradient(circle at 60% 40%, #fff8 0%, #fff0 80%);
  z-index: 1;
  pointer-events: none;
}
.login-brand {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 88px;
  margin-bottom: 10px;
  z-index: 2;
}
.brand-logo-wrap {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: linear-gradient(135deg, #42d0ea 0%, #1677ff 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 24px #1677ff22;
  margin-bottom: 8px;
}
.logo-img {
  width: 48px;
  height: 48px;
  border-radius: 14px;
  background: #fff;
  box-shadow: 0 2px 8px #1677ff22;
}
.brand-name {
  font-size: 22px;
  color: #fff;
  font-weight: bold;
  letter-spacing: 2px;
  text-shadow: 0 2px 8px #1677ff44;
}
.login-header {
  font-size: 22px;
  color: #fff;
  font-weight: bold;
  margin-bottom: 18px;
  letter-spacing: 2px;
  z-index: 2;
  text-shadow: 0 2px 8px #1677ff44;
}
.login-form {
  width: 92vw;
  max-width: 390px;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 8px 32px #1677ff18, 0 2px 8px #42d0ea11;
  padding: 32px 18px 24px 18px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  margin-bottom: 18px;
  border: 1.5px solid #e3f0ff;
  z-index: 2;
  position: relative;
}
.login-tabs {
  display: flex;
  justify-content: center;
  align-items: flex-end;
  margin-bottom: 18px;
  gap: 0;
  position: relative;
  height: 38px;
}
.login-tab {
  font-size: 16px;
  color: #888;
  padding: 4px 32px 8px 32px;
  border-radius: 16px 16px 0 0;
  background: transparent;
  cursor: pointer;
  font-weight: 500;
  transition: color 0.2s, background 0.2s;
  position: relative;
  z-index: 2;
}
.login-tab.active {
  color: #1677ff;
  font-weight: bold;
}
.tab-slider {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 50%;
  height: 4px;
  background: linear-gradient(90deg, #42d0ea 0%, #1677ff 100%);
  border-radius: 2px;
  transition: left 0.3s cubic-bezier(0.4, 2, 0.6, 1);
  z-index: 1;
}
.fade-slide-enter-active,
.fade-slide-leave-active {
  transition: all 0.3s cubic-bezier(0.4, 2, 0.6, 1);
}
.fade-slide-enter,
.fade-slide-leave-to {
  opacity: 0;
  transform: translateY(20px);
}
::v-deep .van-field {
  background: #fff;
  border-radius: 0;
  border: none;
  border-bottom: 1.5px solid #e3f0ff;
  box-shadow: none;
  margin-bottom: 18px;
  padding: 0 2px 0 0;
  transition: border-color 0.2s;
}
::v-deep .van-field--focused {
  border-bottom: 1.5px solid #1677ff;
  background: #fff;
  box-shadow: none;
}
::v-deep .van-field__left-icon {
  color: #b2cfff;
  font-size: 18px;
  background: none;
  padding: 0;
  margin-right: 4px;
}
::v-deep .van-field__label {
  color: #b0b8c9;
  font-size: 13px;
  font-weight: 400;
  padding-right: 6px;
  width: 15%;
}
.login-btn {
  margin-top: 16px;
  font-size: 18px;
  font-weight: bold;
  background: linear-gradient(90deg, #4fc3f7 0%, #1677ff 100%);
  border: none;
  box-shadow: 0 2px 12px #1677ff22;
  transition: box-shadow 0.2s, background 0.2s;
  border-radius: 999px;
  letter-spacing: 2px;
  height: 48px;
}
.login-btn:active {
  box-shadow: 0 1px 4px #1677ff33;
  background: linear-gradient(90deg, #1677ff 0%, #4fc3f7 100%);
}
.login-divider {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 18px 0 8px 0;
  gap: 8px;
}
.divider-line {
  flex: 1;
  height: 1.5px;
  background: linear-gradient(90deg, #e3f0ff 0%, #1677ff22 100%);
  border-radius: 2px;
}
.divider-text {
  color: #bbb;
  font-size: 13px;
  padding: 0 8px;
  font-weight: 500;
}
.login-other {
  display: flex;
  justify-content: center;
  gap: 36px;
  margin-bottom: 2px;
}
.other-btn {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: linear-gradient(135deg, #e3f0ff 0%, #f6f8fa 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 12px #1677ff22;
  cursor: pointer;
  transition: box-shadow 0.2s, transform 0.2s;
  border: none;
  outline: none;
}
.other-btn:active {
  box-shadow: 0 1px 4px #1677ff33;
  transform: scale(0.95);
}
.other-btn.wechat {
  background: linear-gradient(135deg, #4fcf4f 0%, #a8e063 100%);
}
.other-btn.alipay {
  background: linear-gradient(135deg, #1677ff 0%, #42d0ea 100%);
}
.login-footer {
  display: flex;
  justify-content: center;
  gap: 28px;
  margin-top: 8px;
  z-index: 2;
}
.login-link {
  color: #1677ff;
  font-size: 14px;
  cursor: pointer;
  opacity: 0.7;
  transition: opacity 0.2s, text-decoration 0.2s;
}
.login-link:hover {
  opacity: 1;
  text-decoration: underline;
}
</style> 